package com.itcast.demoone.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.RecyclerView
import com.itcast.demoone.R

class ColorAdapter: RecyclerView.Adapter<ColorAdapter.PageViewHolder>() {

    // 数据类
    data class PageData(val color: String, val text: String)

    // 页面数据：颜色 + 文本
    private val pages = listOf(
        PageData("#FF5733", "Page 1"),
        PageData("#33FF57", "Page 2"),
        PageData("#3357FF", "Page 3")
    )
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PageViewHolder {
        return PageViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_page, parent, false))
    }

    override fun onBindViewHolder(
        holder: PageViewHolder,
        position: Int
    ) {
        val pageData = pages[position]
        with(holder) {
            textView.text = pageData.text
            // 设置背景颜色
            itemView.setBackgroundColor(pageData.color.toColorInt())
        }
    }

    override fun getItemCount(): Int {
        return pages.size
    }

    inner class PageViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById<TextView>(R.id.item_page_text)
    }
}